/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package carpool.services;

import carpool.persistence.HibernateUtil;
import carpool.persistence.Pontos;
import java.beans.*;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Session;

/**
 *
 * @author thigu15
 */
public class RideBean implements Serializable {

    public static final String PROP_SAMPLE_PROPERTY = "sampleProperty";

    private String sampleProperty;

    private PropertyChangeSupport propertySupport;

    public RideBean() {
        propertySupport = new PropertyChangeSupport(this);
    }

    public String getSampleProperty() {
        return sampleProperty;
    }

    public void setSampleProperty(String value) {
        String oldValue = sampleProperty;
        sampleProperty = value;
        propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, sampleProperty);
    }


    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.removePropertyChangeListener(listener);
    }

    public Pontos[] searchNearRide(double lat, double lon){


        try{

         double raio = 0.005;

         double lat1 = lat+raio;
         double lat2 = lat-raio;

         double lon1 = lon+raio;
         double lon2 = lon-raio;
         
         Session session = HibernateUtil.getSessionFactory().getCurrentSession();
         session.beginTransaction();

         List result = session.createQuery("from Pontos as ponto where ponto.lat < ? AND ponto.lat > ? AND ponto.lon < ? AND ponto.lat > ?")
                 .setDouble(0, lat1).setDouble(1, lat2).setDouble(2,lon1).setDouble(3,lon2).list();

         if (result.size() == 0){
//             Pontos[] u = new Pontos[1];
//             u[0].setLat(40.2326515);
//             u[0].setLon(-8.4480847);
             return new Pontos[1];
         }
         
         Pontos[] u = new Pontos[result.size()];
         for(int i = 0; (i < result.size() && i < 10); i++){
             u[i] = (Pontos) result.get(i);
         }
         
         session.getTransaction().commit();
         return u;
        }catch(Exception e){
            Logger.getLogger(RideBean.class.getName()).log(Level.SEVERE, null, e);
            return new Pontos[1];
        }
    }
        
    public String toXML(Pontos[] u){

        if(u == null) return "<xml><error>Ponto inexistente</error></xml>";

        StringBuilder sb = new StringBuilder("<xml>");

        for (int i = 0; i < u.length; i++){
            sb.append("<marker>");
            sb.append("<lat>"+u[i].getLat()+"</lat>");
            sb.append("<lon>"+u[i].getLon()+"</lon>");
            sb.append("</marker>");
        }

        sb.append("</xml>");
        //return "<xml><error>Test error</error></xml>";
        return sb.toString();
        //return new String(os.toByteArray(), Charset.defaultCharset()); //"UTF-8");
    }


//        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//        session.beginTransaction();
//
//        List pontosResult = session.createQuery("select p.id from " +
//            "Pontos as p").list();
//
//        Pontos p = (Pontos) pontosResult.get(0);
//
////        p = (Pontos) session.createQuery("from Pontos as p where p.id=?")
////                 .setInteger(0, id)
////                 .uniqueResult();
//        session.getTransaction().commit();
//        session.close();
//        return p;


}
